package com.pearadmin.modules.sys.controller.manage;

import com.github.pagehelper.PageInfo;
import com.pearadmin.common.constant.ControllerConstant;
import com.pearadmin.common.tools.SecurityUtil;
import com.pearadmin.common.tools.SequenceUtil;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.modules.sys.domain.ProducerInfo;
import com.pearadmin.modules.sys.domain.SysUser;
import com.pearadmin.modules.sys.service.IProducerInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;

/**
 * 厂家信息Controller
 *
 * @author Mr.Ling
 * @date 2021-11-26
 */
@RestController
@Api(tags = {"厂家信息"})
@RequestMapping(ControllerConstant.API_MANAGE_PREFIX + "producerInfo")
public class ProducerInfoController extends BaseController {
    private String prefix = "manage/producerInfo" ;

    @Autowired
    private IProducerInfoService producerInfoService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/manage/producerInfo/main','manage:producerInfo:main')")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询厂家信息列表
     */
    @ResponseBody
    @ApiOperation("列表")
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/manage/producerInfo/data','manage:producerInfo:data')")
    public ResultTable list(@ModelAttribute ProducerInfo producerInfo, PageDomain pageDomain) {
        PageInfo<ProducerInfo> pageInfo = producerInfoService.selectProducerInfoPage(producerInfo, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 新增厂家信息
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/manage/producerInfo/add','manage:producerInfo:add')")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存厂家信息
     */
      @ResponseBody
    @ApiOperation("新增")
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/manage/producerInfo/add','manage:producerInfo:add')")
    public Result save(@RequestBody ProducerInfo producerInfo) {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        producerInfo.setCreateTime(LocalDateTime.now());
        producerInfo.setCreateBy(sysUser.getUserId());
        producerInfo.setCreateName(sysUser.getRealName());
        producerInfo.setId(SequenceUtil.makeStringId());
        return decide(producerInfoService.insertProducerInfo(producerInfo));
    }

    /**
     * 修改厂家信息
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/manage/producerInfo/edit','manage:producerInfo:edit')")
    public ModelAndView edit(String id, ModelMap mmap) {
        ProducerInfo producerInfo = producerInfoService.selectProducerInfoById(id);
        producerInfo.setUpdateTime(LocalDateTime.now());
        mmap.put("producerInfo", producerInfo);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存厂家信息
     */
    @ResponseBody
    @ApiOperation("更新")
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/manage/producerInfo/edit','manage:producerInfo:edit')")
    public Result update(@RequestBody ProducerInfo producerInfo) {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        producerInfo.setUpdateTime(LocalDateTime.now());
        producerInfo.setUpdateBy(sysUser.getUserId());
        producerInfo.setUpdateName(sysUser.getUsername());
        return decide(producerInfoService.updateProducerInfo(producerInfo));
    }

    /**
     * 删除厂家信息
     */
    @ResponseBody
    @ApiOperation("批量刪除")
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/manage/producerInfo/remove','manage:producerInfo:remove')")
    public Result batchRemove(String ids) {
        return decide(producerInfoService.deleteProducerInfoByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @ApiOperation("根据id 刪除")
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/manage/producerInfo/remove','manage:producerInfo:remove')")
    public Result remove(@PathVariable("id") String id) {
        return decide(producerInfoService.deleteProducerInfoById(id));
    }
}
